package edu.sctu.zcy.month201506.day20150628test;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;


import java.io.*;
import java.util.List;

/**
 * Created by user24 on 2015/6/28.
 */
public class HttpPostUtil extends Thread {

    public static String message;

    public void run(){

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
      sendPost(URLconfig.URL_PATH,Params.getParamsList("1","10"));
    }

    public String sendPost(String path,List<NameValuePair> paramsList) {

        HttpClientBuilder builder = HttpClientBuilder.create();
        CloseableHttpClient client = builder.build();

        HttpPost httpPost = new HttpPost(path);

        httpPost.setEntity(new UrlEncodedFormEntity(paramsList, Consts.UTF_8));

        try {
            HttpResponse response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();

            message = EntityUtils.toString(entity);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return message;
    }
}
